<?php    
session_start();
$custid = isset($_REQUEST['custid']) ? $_REQUEST['custid'] : "";
$break = isset($_REQUEST['break']) ? $_REQUEST['break'] : "medium";
$article1 = isset($_REQUEST['article1']) ? $_REQUEST['article1'] : "";
$article2 = isset($_REQUEST['article2']) ? $_REQUEST['article2'] : "";
$boxqty = isset($_REQUEST['boxqty']) ? $_REQUEST['boxqty'] : 0;
$actqty = isset($_REQUEST['actqty']) ? $_REQUEST['actqty'] : 0;
$newdate = isset($_REQUEST['newdate']) ? $_REQUEST['newdate'] : 0;
$rest = isset($_REQUEST['rest']) ? $_REQUEST['rest'] : 0;
$i = $actqty + 1; // initial value
$langpair = $_SESSION['langpair'];

require ('konfiguration.html');

    /*    // check, if maxqty is exeeded
        $sql = "select actqty, maxqty, sevendays from settings where customerid = ". $custid;
        $sql2 = "select 1word from wordbox0 where customerid = ". $custid . " and boxid = ". $_SESSION['actbox'] . " and langpairid = ". $langpair . ' LIMIT 0, '. $rest;
		$db_sel = mysql_query( $sql );
        if ( ! $db_sel ) { die('Error query: ' . mysql_error()); }
        $db_sel2 = mysql_query( $sql2 );
        if ( ! $db_sel2 ) { die('Error query: ' . mysql_error()); }
        $today = mktime(); // now
        $boxqty = mysql_num_rows($db_sel2);
        while ($row = mysql_fetch_array( $db_sel, MYSQL_ASSOC)) {
        $actqty = $row['actqty'];
        $sevendays = $row['sevendays']; // 7 days timelimit
        // convert into timestamp
        $hour = substr($sevendays,11,2);
        $minute = substr($sevendays,14,2);
        $second = substr($sevendays,17,2);
        $month = substr($sevendays,5,2);
        $day = substr($sevendays,8,2);
        $year = substr($sevendays,0,4);
        $mktime = mktime($hour, $minute, $second, $month, $day, $year);
        // difference between now and 7 days limit, rounding down
        $diff = round(($today-$mktime) / (3600*24),0);
        //    floor($diff);
        if ( $diff >= 7 ) { $actqty = 0; $newdate = 1; }
        if ( $actqty + $boxqty <= $row['maxqty'] )
        {
*/

// zip file
include("zip.lib.html");
$ziper = new zipfile();

mysql_query('SET NAMES utf8');
mysql_query('SET CHARACTER SET utf8');
$sql = "select 1id,2id,3id,id1,id2,id3,langpairid,langid,1art,1word,2art,2word,3art,3word,art1,word1,art2,word2,art3,word3 from wordbox0 where customerid = ". $custid . " and langpairid =". $langpair . " and boxid =  ". $_SESSION['actbox']  . ' order by timestamp LIMIT '. $rest;
$db_sel = mysql_query( $sql );
if ( ! $db_sel ) { die('Error query: ' . mysql_error()); }

while ($row = mysql_fetch_array( $db_sel, MYSQL_ASSOC)) 
{
    // initialize variables
    $argv[21] = '';
    $argv[22] = '';
    $argv[23] = '';
    $argv[31] = '';
    $argv[32] = '';
    $argv[33] = '';
    
    // estimation language lango1 = to learn ; lango2 = I can 
    switch ( $row['langpairid'] ) {
        case '1' : $lango2 = 'ger1443'; $lango1 = 'eng2573'; break;
        case '2' : $lango2 = 'ger1443'; $lango1 = 'spa7023'; break;
        case '3' : $lango2 = 'eng2573'; $lango1 = 'spa7023'; break;
        case '4' : $lango2 = 'eng2573'; $lango1 = 'ger1443'; break;
        case '5' : $lango2 = 'spa7023'; $lango1 = 'eng2573'; break;
        case '6' : $lango2 = 'spa7023'; $lango1 = 'ger1443'; break;
        case '7' : $lango2 = 'ger1443'; $lango1 = 'rus7261'; break;
        case '8' : $lango2 = 'rus7261'; $lango1 = 'ger1443'; break;
    } 
    // breaks
    if ( $break == 'medium' ) 
    { $argv[0] = "../mp3/generaI243/mediumbreak.mp3"; }
    elseif ( $break == 'long' )
    { $argv[0] = "../mp3/generaI243/longbreak.mp3"; }
    elseif ( $break == 'short' )
    { $argv[0] = "../mp3/generaI243/shortbreak.mp3"; }


    // articles
    if ( $article1 == 'language1' )
    {    
        if ( $row['langid'] == '1' )
        {
            if ( $row['art1'] <> '' ) { if ( $row['id1'] == 0 ) { $argv[31] = "../mp3/generaI243/". $row['art1'] . ".mp3"; } elseif ( $row['id1'] = 1 ) { $argv[31] = "../mp3/generaI243/". $row['art1'] . $row['id1'] . ".mp3"; } }
            if ( $row['art2'] <> '' ) { if ( $row['id2'] == 0 ) { $argv[32] = "../mp3/generaI243/". $row['art2'] . ".mp3"; } elseif ( $row['id2'] = 1 ) { $argv[32] = "../mp3/generaI243/". $row['art2'] . $row['id2'] . ".mp3"; } }
            if ( $row['art3'] <> '' ) { if ( $row['id3'] == 0 ) { $argv[33] = "../mp3/generaI243/". $row['art3'] . ".mp3"; } elseif ( $row['id3'] = 1 ) { $argv[33] = "../mp3/generaI243/". $row['art3'] . $row['id3'] . ".mp3"; } }
        }    
        elseif ( $row['langid'] == '2' )
        {
            if ( $row['1art'] <> '' ) { if ( $row['1id'] == 0 ) { $argv[21] = "../mp3/generaI243/". $row['1art'] . ".mp3"; } elseif ( $row['1id'] = 1 ) { $argv[21] = "../mp3/generaI243/". $row['1art'] . $row['1id'] . ".mp3"; } }
            if ( $row['2art'] <> '' ) { if ( $row['2id'] == 0 ) { $argv[22] = "../mp3/generaI243/". $row['2art'] . ".mp3"; } elseif ( $row['2id'] = 1 ) { $argv[22] = "../mp3/generaI243/". $row['2art'] . $row['2id'] . ".mp3"; } }
            if ( $row['3art'] <> '' ) { if ( $row['3id'] == 0 ) { $argv[23] = "../mp3/generaI243/". $row['3art'] . ".mp3"; } elseif ( $row['3id'] = 1 ) { $argv[23] = "../mp3/generaI243/". $row['3art'] . $row['3id'] . ".mp3"; } }
        }
    }
    
       if ( $article2 == 'language2' )
    {
        if ( $row['langid'] == '1' )
        {
            if ( $row['1art'] <> '' ) { if ( $row['1id'] == 0 ) { $argv[21] = "../mp3/generaI243/". $row['1art'] . ".mp3"; } elseif ( $row['1id'] = 1 ) { $argv[21] = "../mp3/generaI243/". $row['1art'] . $row['1id'] . ".mp3"; } }
            if ( $row['2art'] <> '' ) { if ( $row['2id'] == 0 ) { $argv[22] = "../mp3/generaI243/". $row['2art'] . ".mp3"; } elseif ( $row['2id'] = 1 ) { $argv[22] = "../mp3/generaI243/". $row['2art'] . $row['2id'] . ".mp3"; } }
            if ( $row['3art'] <> '' ) { if ( $row['3id'] == 0 ) { $argv[23] = "../mp3/generaI243/". $row['3art'] . ".mp3"; } elseif ( $row['3id'] = 1 ) { $argv[23] = "../mp3/generaI243/". $row['3art'] . $row['3id'] . ".mp3"; } }
        }
        elseif ( $row['langid'] == '2' )
        {
            if ( $row['art1'] <> '' ) { if ( $row['id1'] == 0 ) { $argv[31] = "../mp3/generaI243/". $row['art1'] . ".mp3"; } elseif ( $row['id1'] = 1 ) { $argv[31] = "../mp3/generaI243/". $row['art1'] . $row['id1'] . ".mp3"; } }
            if ( $row['art2'] <> '' ) { if ( $row['id2'] == 0 ) { $argv[32] = "../mp3/generaI243/". $row['art2'] . ".mp3"; } elseif ( $row['id2'] = 1 ) { $argv[32] = "../mp3/generaI243/". $row['art2'] . $row['id2'] . ".mp3"; } }
            if ( $row['art3'] <> '' ) { if ( $row['id3'] == 0 ) { $argv[33] = "../mp3/generaI243/". $row['art3'] . ".mp3"; } elseif ( $row['id3'] = 1 ) { $argv[33] = "../mp3/generaI243/". $row['art3'] . $row['id3'] . ".mp3"; } }
         }
    }
/*echo '21:&nbsp;';
echo $argv[21];
echo '<br />';
echo '22:&nbsp;';
echo $argv[21];
echo '<br />';
echo '23:&nbsp;';
echo $argv[21];
echo '<br />';
echo '31:&nbsp;';
echo $argv[21];
echo '<br />';
echo '32:&nbsp;';
echo $argv[21];
echo '<br />';
echo '33:&nbsp;';
echo $argv[21];
echo '<br />';
    echo '<br />';
    echo $break;
    echo '<br />';
    echo $article1;
    echo '<br />';
    echo $article2;
    echo '<br />';
    echo $row['1word'];
    echo '<br />';
    echo $row['word1'];
    echo '<br />';   */
    
    if ( $row['langid'] == '2' )
    {    
        $argv[1] = "../mp3/". $lango1 . "/". $row['1word'] . ".mp3";
        if ( $row['2word'] <> "" ) 
        {
            $argv[2] = "../mp3/". $lango1 . "/". $row['2word'] . ".mp3";
        }
        else $argv[2] = "";
        if ( $row['3word'] <> "" )
        {
            $argv[7] = "../mp3/". $lango1 . "/". $row['3word'] . ".mp3";
        }
        else $argv[7] = "";
        $argv[3] = "../mp3/". $lango2 . "/". $row['word1'] . ".mp3";
        if ( $row['word2'] <> "" )
        {
            $argv[4] = "../mp3/". $lango2 . "/". $row['word2'] . ".mp3";
        }
        else $argv[4] = "";
        if ( $row['word3'] <> "" )
        {
            $argv[8] = "../mp3/". $lango2 . "/". $row['word3'] . ".mp3";
        }
        else $argv[8] = "";
        $argv[5] = "lingomoto/lingomoto". $i . ".mp3";
        $argv[6] = "../mp3/generaI243/smallbreak.mp3";
    }
    else
    {  
        $argv[1] = "../mp3/". $lango2 . "/". $row['1word'] . ".mp3";
        if ( $row['2word'] <> "" )
        {
            $argv[2] = "../mp3/". $lango2 . "/". $row['2word'] . ".mp3";
        }
        else $argv[2] = "";
        if ( $row['3word'] <> "" )
        {
            $argv[7] = "../mp3/". $lango2 . "/". $row['3word'] . ".mp3";
        }
        else $argv[7] = "";
        $argv[3] = "../mp3/". $lango1 . "/". $row['word1'] . ".mp3";
        if ( $row['word2'] <> "" )
        {
            $argv[4] = "../mp3/". $lango1 . "/". $row['word2'] . ".mp3";
        }
        else $argv[4] = "";
        if ( $row['word3'] <> "" )
        {
            $argv[8] = "../mp3/". $lango1 . "/". $row['word3'] . ".mp3";
        }
        else $argv[8] = "";
        $argv[5] = "lingomoto/lingomoto". $i . ".mp3";
        $argv[6] = "../mp3/generaI243/smallbreak.mp3";
    } 

	// delete abbreviations (can only be in argv1 or argv3, as these are the words you want to learn)
	$argv[1] = str_replace(" (f)", "", $argv[1]);
	$argv[1] = str_replace(" (m)", "", $argv[1]);
	$argv[1] = str_replace(" (adj)", "", $argv[1]);
	$argv[1] = str_replace(" (vb)", "", $argv[1]);
	$argv[1] = str_replace(" (n)", "", $argv[1]);
	$argv[1] = str_replace(" (adv)", "", $argv[1]);
	$argv[3] = str_replace(" (f)", "", $argv[3]);
	$argv[3] = str_replace(" (m)", "", $argv[3]);
	$argv[3] = str_replace(" (adj)", "", $argv[3]);
	$argv[3] = str_replace(" (vb)", "", $argv[3]);
	$argv[3] = str_replace(" (n)", "", $argv[3]);
	$argv[3] = str_replace(" (adv)", "", $argv[3]);
	
    // connect mp3-files
    error_reporting(E_ALL);

    $file0 = isset($argv[0]) ? $argv[0] : die('Fehlendes Argument (Break)');
    $file1 = isset($argv[1]) ? $argv[1] : die('Fehlendes Argument (Datei 1)');
    $file2 = isset($argv[2]) ? $argv[2] : "";
    $file3 = isset($argv[3]) ? $argv[3] : die('Fehlendes Argument (Datei 3)');
    $file4 = isset($argv[4]) ? $argv[4] : "";
    $file5 = isset($argv[5]) ? $argv[5] : die('Fehlendes Argument (Ausgabedatei)');
    $file6 = isset($argv[6]) ? $argv[6] : die('Fehlendes Argument (Break)');
    $file7 = isset($argv[7]) ? $argv[7] : "";
    $file8 = isset($argv[8]) ? $argv[8] : "";

    if ( $argv[21] <> '' ) { $file21 = $argv[21]; } else { $file21 = "../mp3/generaI243/nobreak.mp3"; }
    if ( $argv[22] <> '' ) { $file22 = $argv[22]; } else { $file22 = "../mp3/generaI243/nobreak.mp3"; }
    if ( $argv[23] <> '' ) { $file23 = $argv[23]; } else { $file23 = "../mp3/generaI243/nobreak.mp3"; }
    if ( $argv[31] <> '' ) { $file31 = $argv[31]; } else { $file31 = "../mp3/generaI243/nobreak.mp3"; }
    if ( $argv[32] <> '' ) { $file32 = $argv[32]; } else { $file32 = "../mp3/generaI243/nobreak.mp3"; }
    if ( $argv[33] <> '' ) { $file33 = $argv[33]; } else { $file33 = "../mp3/generaI243/nobreak.mp3"; }

   /* $file1 = utf8_encode($file1);
    $file2 = utf8_encode($file2);
    $file3 = utf8_encode($file3);
    $file4 = utf8_encode($file4);
    $file7 = utf8_encode($file7);
    $file8 = utf8_encode($file8);
    

   $fl = file_get_contents($file1); 
    $nLen = strlen($fl);
    $fl = substr($fl, 1);    
    
    
        echo $nLen;
$str = substr($file1, $nLen-128);   // Die letzten 128 Zeichen
    $str = substr($str, 0, 3);               // und davon die ersten 3 Zeichen.
    $tag = strtolower($str);
    echo $tag; */
                // Wenn hier 'tag' steht,    
    /* echo $file1;
    echo '<br />';
    echo $file3;
    echo '<br />';  */ 

    is_file($file1) && is_file($file3) OR die('Falsches Argument (Keine Datei)');
    
    touch($file5) OR die('Fehler beim Anlegen der neuen Datei');

    // without articles
    if ( $article1 == '' and $article2 == '' )
    {
    if ( $file2 == "" and $file4 == "" )
        { file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file0) )
            OR die('Fehler beim Schreiben der neuen Datei'); }
    elseif ( $file2 == "" and $file4 <> "" )
        { 
            if ( $file8 == "" )
            {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file6).file_get_contents($file32).file_get_contents($file4).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei'); 
            }
            else
            {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file6).file_get_contents($file32).file_get_contents($file4).file_get_contents($file6).file_get_contents($file33).file_get_contents($file8).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
            }
        }
    elseif ( $file2 <> "" and $file4 == "" )
        { 
            if ( $file7 == "" )
            {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file6).file_get_contents($file22).file_get_contents($file2).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei'); 
            }
            else
            {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file6).file_get_contents($file22).file_get_contents($file2).file_get_contents($file6).file_get_contents($file23).file_get_contents($file7).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
            }
        }
    else {
        file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file6).file_get_contents($file22).file_get_contents($file2).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file6).file_get_contents($file32).file_get_contents($file4).file_get_contents($file0) )
            OR die('Fehler beim Schreiben der neuen Datei'); }

    $ziper->addFiles(array("lingomoto/lingomoto". $i . ".mp3"));  //array of files

    $i++; // increase value
    if ( $i > 100 ) { $i = 1; }
    }
    
    // articles for language1 AND language2
    elseif ( $article1 == 'language1' and $article2 == 'language2' )
    {
    if ( $file2 == "" and $file4 == "" )
        { file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file0) )
            OR die('Fehler beim Schreiben der neuen Datei'); }
    elseif ( $file2 == "" and $file4 <> "" )
        {
            if ( $file8 == "" )
            {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file6).file_get_contents($file32).file_get_contents($file4).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
            }
            else
            {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file6).file_get_contents($file32).file_get_contents($file4).file_get_contents($file6).file_get_contents($file33).file_get_contents($file8).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
            }
        }
    elseif ( $file2 <> "" and $file4 == "" )
        {
            if ( $file7 == "" )
            {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file6).file_get_contents($file22).file_get_contents($file2).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
            }
            else
            {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file6).file_get_contents($file22).file_get_contents($file2).file_get_contents($file6).file_get_contents($file23).file_get_contents($file7).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
            }
        }
    else {
        file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file6).file_get_contents($file22).file_get_contents($file2).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file6).file_get_contents($file32).file_get_contents($file4).file_get_contents($file0) )
            OR die('Fehler beim Schreiben der neuen Datei'); }

    $ziper->addFiles(array("lingomoto/lingomoto". $i . ".mp3"));  //array of files

    $i++; // increase value
    if ( $i > 100 ) { $i = 1; }
    }

    // articles for only language1 
    elseif ( $article1 == 'language1' and $article2 == '' )
    {
    if ( $file2 == "" and $file4 == "" )
        { file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file0) )
            OR die('Fehler beim Schreiben der neuen Datei'); }
    elseif ( $file2 == "" and $file4 <> "" )
        {
            if ( $file8 == "" )
            {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file6).file_get_contents($file32).file_get_contents($file4).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
            }
            else
            {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file6).file_get_contents($file32).file_get_contents($file4).file_get_contents($file6).file_get_contents($file33).file_get_contents($file8).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
            }
        }
    elseif ( $file2 <> "" and $file4 == "" )
        {
            if ( $file7 == "" )
            {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file6).file_get_contents($file22).file_get_contents($file2).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
            }
            else
            {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file6).file_get_contents($file22).file_get_contents($file2).file_get_contents($file6).file_get_contents($file23).file_get_contents($file7).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
            }
        }
    else {
        file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file6).file_get_contents($file22).file_get_contents($file2).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file6).file_get_contents($file32).file_get_contents($file4).file_get_contents($file0) )
            OR die('Fehler beim Schreiben der neuen Datei'); }

    $ziper->addFiles(array("lingomoto/lingomoto". $i . ".mp3"));  //array of files

    $i++; // increase value
    if ( $i > 100 ) { $i = 1; }
    }

    // articles for only language2
    elseif ( $article1 == '' and $article2 == 'language2' )
    {
    if ( $file2 == "" and $file4 == "" )
        { file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file0) )
            OR die('Fehler beim Schreiben der neuen Datei'); }
    elseif ( $file2 == "" and $file4 <> "" )
        {
            if ( $file8 == "" )
            {
                if ( $row['langid'] == '2' ) 
                {
                    file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file6).file_get_contents($file32).file_get_contents($file4).file_get_contents($file0) )
                    OR die('Fehler beim Schreiben der neuen Datei');
                }
                elseif ( $row['langid'] == '1' )
                {
                 /*   file_put_contents( $file5, $fl);  */
                    file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file6).file_get_contents($file32).file_get_contents($file4).file_get_contents($file0) )
                    OR die('Fehler beim Schreiben der neuen Datei'); 
                }
            }
            else
            {
                if ( $row['langid'] == '2' )
                {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file6).file_get_contents($file32).file_get_contents($file4).file_get_contents($file6).file_get_contents($file33).file_get_contents($file8).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
                }
                elseif ( $row['langid'] == '1' )
                {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file6).file_get_contents($file32).file_get_contents($file4).file_get_contents($file6).file_get_contents($file33).file_get_contents($file8).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
                }
             }
        }
    elseif ( $file2 <> "" and $file4 == "" )
        {
            if ( $file7 == "" )
            {
                if ( $row['langid'] == '2' )
                {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file6).file_get_contents($file22).file_get_contents($file2).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
                }
                elseif ( $row['langid'] == '1' )
                {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file6).file_get_contents($file22).file_get_contents($file2).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
                }
            }
            else
            {
                if ( $row['langid'] == '2' )
                {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file6).file_get_contents($file22).file_get_contents($file2).file_get_contents($file6).file_get_contents($file23).file_get_contents($file7).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
                }
                elseif ( $row['langid'] == '1' )
                {
                file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file6).file_get_contents($file22).file_get_contents($file2).file_get_contents($file6).file_get_contents($file23).file_get_contents($file7).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file0) )
                OR die('Fehler beim Schreiben der neuen Datei');
                }
            }
        }
    else {
        file_put_contents( $file5, file_get_contents($file6).file_get_contents($file21).file_get_contents($file1).file_get_contents($file6).file_get_contents($file22).file_get_contents($file2).file_get_contents($file0).file_get_contents($file31).file_get_contents($file3).file_get_contents($file6).file_get_contents($file32).file_get_contents($file4).file_get_contents($file0) )
            OR die('Fehler beim Schreiben der neuen Datei'); }

    $ziper->addFiles(array("lingomoto/lingomoto". $i . ".mp3"));  //array of files

    $i++; // increase value
    if ( $i > 100 ) { $i = 1; }
    }

}   

$ziper->output("lernbox". $custid . ".zip");

$file = 'lernbox'. $custid . '.zip';
$dir = '/is/htdocs/wp1153130_KAL1VJ4KYQ/www/de/';
$type = 'application/zip';

// if(!empty($_GET['file']) && !preg_match('=/=', $_GET['file'])) {
if(file_exists ($dir.$file))     {
        makeDownload($file, $dir, $type);

    // update actqty
    if ( $newdate == 1 ) 
    {
        $seven = date("ymdhis");
		
		if ( $boxqty + $actqty > 100 ) {
			$sql2 = "update settings set actqty = ". $actqty . " + ". $rest . ", sevendays = ". $seven . " where customerid = ". $custid;
		} else {
			$sql2 = "update settings set actqty = ". $actqty . " + ". $boxqty . ", sevendays = ". $seven . " where customerid = ". $custid;
		}
	}
    else
    {
		if ( $boxqty + $actqty > 100 ) {
			$sql2 = "update settings set actqty = ". $actqty . " + ". $rest . " where customerid = ". $custid;
		} else {
			$sql2 = "update settings set actqty = ". $actqty . " + ". $boxqty . " where customerid = ". $custid;
		}
    }
    $db_upd = mysql_query( $sql2 );
    if ( ! $db_upd ) { die('Error query: ' . mysql_error()); }

	// clear lastdownload before
	$sql7 = " update wordbox0 set lastdownload = 0 ";
    $sql7 .= " where customerid = ". $custid . " and langpairid =". $langpair;
	$db_sel7 = mysql_query( $sql7 );
    if ( ! $db_sel7 )
       { die('Error query: ' . mysql_error()); }
	
	
	// update timestamp and lastdownload
	$sql1 = " update wordbox0 set timestamp = current_timestamp, lastdownload = ". $_SESSION['actbox']  ." ";
    $sql1 .= " where customerid = ". $custid . " and langpairid =". $langpair . " and boxid =  ". $_SESSION['actbox']  . ' order by timestamp LIMIT '. $rest;
	$db_sel = mysql_query( $sql1 );
    if ( ! $db_sel )
       { die('Error query: ' . mysql_error()); }
	
}



?>